797B - Odd sum - CodeForces Solution


dp greedy implementation *1400

Please click on ads to support us..

Python Code:

n = int(input())
arr = list(map(int, input().split()))

totalSum = 0
for i in arr:
    if (i > 0):
        totalSum += i

if (totalSum % 2 == 0):
    minOdd = 10**9
    for i in arr:
        if (abs(i) % 2 == 1 and abs(i) < minOdd):
            minOdd = abs(i)

    totalSum -= minOdd
print(totalSum)

C++ Code:

#include<bits/stdc++.h>
using namespace std;

void solution() {
    int n;
    cin >> n;
    long long int arr[n];
    for (int i=0; i<n; i++)
        cin >> arr[i];
    sort(arr, arr+n);
    long long int sum = 0;
    long long int lastOdd = INT_MAX;
    for (int i=n-1; i>=0; i--) {
        if (arr[i]%2) lastOdd = min(lastOdd, abs(arr[i]));
        if (arr[i] > 0)
            sum += arr[i];
    }
    if (sum%2 == 0)
        sum -= lastOdd;
    cout << sum << endl;
}


int main() {
    solution();
}

	  		  	 	  			  	   			  		 		


Comments

Submit
0 Comments
More Questions

320A - Magic Numbers
1658A - Marin and Photoshoot
514A - Chewbaсca and Number
382A - Ksenia and Pan Scales
734B - Anton and Digits
1080A - Petya and Origami
1642D - Repetitions Decoding
1440A - Buy the String
1658F - Juju and Binary String
478A - Initial Bet
981A - Antipalindrome
365A - Good Number
1204B - Mislove Has Lost an Array
1409D - Decrease the Sum of Digits
1476E - Pattern Matching
1107A - Digits Sequence Dividing
1348A - Phoenix and Balance
1343B - Balanced Array
1186A - Vus the Cossack and a Contest
1494A - ABC String
1606A - AB Balance
1658C - Shinju and the Lost Permutation
1547C - Pair Programming
550A - Two Substrings
797B - Odd sum
1093A - Dice Rolling
1360B - Honest Coach
1399C - Boats Competition
1609C - Complex Market Analysis
1657E - Star MST